package com.pcbly.web.action.calendar;

import java.util.Date;

import com.pcbly.web.action.BaseAction;
import com.pcbly.web.bean.calendar.CalendarEventBean;
import com.pcbly.web.business.calendar.CalendarEventService;
import com.pcbly.web.components.hibernate.support.Page;

/**
* @author carl.song
* Copyright (c) 2010 Lightsoft, International.
* All rights reserved.
*
*/

public class CalendarEventAction extends BaseAction<CalendarEventBean> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2414843342481379023L;

	protected CalendarEventService calendarEventService; 
	
	public void setCalendarEventService(CalendarEventService calendarEventService) {
		this.calendarEventService = calendarEventService;
	}
	private CalendarEventBean calendarEvent;
	
	private String orderBy;
	
	private Page<CalendarEventBean> pages;
	
	private boolean asc = true;
	
	public boolean isAsc() {
		return asc;
	}

	public void setAsc(boolean asc) {
		this.asc = asc;
	}
	
	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}
	
	public CalendarEventBean getCalendarEvent() {
		return calendarEvent;
	}
	public Page<CalendarEventBean> getPages() {
		return pages;
	}

	public void setPages(Page<CalendarEventBean> pages) {
		this.pages = pages;
	}
	public void setCalendarEvent(CalendarEventBean calendarEvent) {
		this.calendarEvent = calendarEvent;
	}
	public String view() throws Exception {
		long startTime = System.currentTimeMillis();
		calendarEvent=calendarEventService.getCalendarEventBeanById(getId());
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.view();
	}
	public String update() throws Exception {
		long startTime = System.currentTimeMillis();
		setMyMessage(null);
		calendarEvent=calendarEventService.getCalendarEventBeanById(getId());
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.update();
	}
	public String index() throws Exception {
		long startTime = System.currentTimeMillis();

		long start = getPage();
		start = start * getPageSize();
		
		if (orderBy == null || "".equals(orderBy)) {
			orderBy = " id ";
		}
		pages = new Page<CalendarEventBean>();
		pages.setStart(start);
		pages.setPageSize(getPageSize());
		pages = calendarEventService.findCalendarEventBeanByCond(
				calendarEvent, pages, orderBy, asc);
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("page:::::" + getPage());
			log.debug("pageSize:::::" + getPageSize());
			log.debug("time ms:::::" + startTime);

			System.out.println("page:::::" + getPage());
			System.out.println("pageSize:::::" + getPageSize());
			System.out.println("time ms:::::" + startTime);
		}
		return super.index();
	}
	public String add() throws Exception { 
		long startTime = System.currentTimeMillis();
		setMyMessage(null);
		calendarEvent=new CalendarEventBean();
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.add();
	}
	public String save() throws Exception {
		long startTime = System.currentTimeMillis();
		if (log.isDebugEnabled()) {
			log.debug("login save..........");
			log.debug(calendarEvent.getId());
		}
		try{
		if (calendarEvent.getId() == null || "".equals(calendarEvent.getId().trim())) {// new 
			calendarEvent.setCreateDate(new Date());
			calendarEvent.setCreateUserId(getUserId());
			calendarEvent.setRecycled("0");
			calendarEvent=calendarEventService.saveCalendarEventBean(calendarEvent);
			setMyMessage(getText("entity.saved"));
		} else {// update
			calendarEventService.updateCalendarEventBean(calendarEvent);
			setMyMessage(getText("entity.updated"));
		}}catch(Exception e){
			e.printStackTrace();
		}
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.save();
	}
	public String dwrSave(CalendarEventBean bean) throws Exception {
		long startTime = System.currentTimeMillis();
		if (log.isDebugEnabled()) {
			log.debug("login save..........");
			log.debug(bean.getId());
		}
		try{
		if (bean.getId() == null || "".equals(bean.getId())) {// new 
			calendarEventService.saveCalendarEventBean(bean);
		} else {// update
			calendarEventService.updateCalendarEventBean(bean);
		}}catch(Exception e){
			e.printStackTrace();
		}
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.dwrBaseSave(bean.getId());
	}
	public String delete() throws Exception {
		long startTime = System.currentTimeMillis();
		if (log.isDebugEnabled()) {
			log.debug("login delete..........");
			log.debug(getId());
		}
		if(getId().indexOf(",")>=0){
			calendarEventService.removeCalendarEventBeanByIds(getId());
		}else{
			calendarEventService.removeCalendarEventBeanById(getId());
		}
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		calendarEvent=null;
		return super.delete();
	}
	public String dwrDelete(String id) throws Exception {
		long startTime = System.currentTimeMillis();
		if (log.isDebugEnabled()) {
			log.debug("login delete..........");
			log.debug(id);
		}
		if(id.indexOf(",")>=0){
			calendarEventService.removeCalendarEventBeanByIds(id);
		}else{
			calendarEventService.removeCalendarEventBeanById(id);
		}
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.dwrBaseDelete(id);
	}
	
}